package com.csu.cphone.controller;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.csu.common.exception.WebException;
import com.csu.common.vo.ResultVo;
import com.csu.common.vo.WxJsapiTicketSignVo;
import com.csu.common.wx.WxJsapiTiketHelper;
import com.csu.cphone.pojo.StorePojo;
import com.csu.cphone.service.LoactionService;
import com.csu.cphone.service.StoreService;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;

/**
 * @项目名称：cphone
 * @类名称：StoreController
 * @类描述：
 * @创建人：Tangc
 * @创建时间：2017年9月12日 下午2:10:07
 * @company:
 */
@Controller
@RequestMapping("/storeinfo")
@Api(value = "base", description = "基础信息")
public class BaseController
{
    @Autowired
    private LoactionService loactionService;

    @Autowired
    private StoreService storeService;

    @ResponseBody
    @ApiOperation(value = "门店信息列表", notes = "返回附近的门店信息列表")
    @RequestMapping(value = "nearlist/{lat}/{lng}", method = RequestMethod.GET, produces = { "application/json; charset=utf-8" })
    public ResultVo<List<StorePojo>> nearlist(HttpServletRequest request,
            @ApiParam(value = "经度") @PathVariable(value = "lng") double lng,
            @ApiParam(value = "纬度") @PathVariable(value = "lat") double lat) throws WebException
    {
        ResultVo<List<StorePojo>> result = new ResultVo<List<StorePojo>>();
        List<StorePojo> storeList = loactionService.getNearlistStore(lng, lat);

        result.setData(storeList);
        return result;

    }

    @ResponseBody
    @ApiOperation(value = "查询门店信息", notes = "单个门店信息")
    @RequestMapping(value = "storeInfo/{storeId}", method = RequestMethod.GET, produces = { "application/json; charset=utf-8" })
    public ResultVo<StorePojo> storeInfo(HttpServletRequest request,
            @ApiParam(value = "门店id") @PathVariable(value = "storeId") Integer storeId) throws WebException
    {
        ResultVo<StorePojo> result = new ResultVo<StorePojo>();
        StorePojo store = storeService.queryById(storeId);

        result.setData(store);
        return result;

    }

    @ResponseBody
    @ApiOperation(value = "添加门店信息", notes = "添加门店信息")
    @RequestMapping(value = "addStore", method = { RequestMethod.POST, RequestMethod.GET }, produces = { "application/json; charset=utf-8" })
    public ResultVo<StorePojo> addStore(HttpServletRequest request,
            @ApiParam(value = "联系电话") @RequestParam(value = "number", required = true) String number,
            @ApiParam(value = "经度") @RequestParam(value = "lng", required = true) Double lng,
            @ApiParam(value = "纬度") @RequestParam(value = "lat", required = true) Double lat,
            @ApiParam(value = "门店名称") @RequestParam(value = "name", required = true) String name,
            @ApiParam(value = "门店地址") @RequestParam(value = "address", required = true) String address,
            @ApiParam(value = "服务方id") @RequestParam(value = "producerId", required = true) Integer producerId)
            throws WebException
    {
        ResultVo<StorePojo> result = new ResultVo<StorePojo>();
        StorePojo storepojo = loactionService.addStore(number, lng, lat, name, address, producerId);
        result.setData(storepojo);
        return result;

    }

    @ResponseBody
    @ApiOperation(value = "查询Jsapi权限签名算法对象", notes = "查询Jsapi权限签名算法对象")
    @RequestMapping(value = "jsapiSign", method = RequestMethod.GET, produces = { "application/json; charset=utf-8" })
    public ResultVo<WxJsapiTicketSignVo> jsapiSign(HttpServletRequest request,
            @ApiParam(value = "url") @RequestParam(value = "url") String url) throws WebException,
            UnsupportedEncodingException
    {
        ResultVo<WxJsapiTicketSignVo> result = new ResultVo<WxJsapiTicketSignVo>();
        url = URLDecoder.decode(url, "UTF-8");
        WxJsapiTicketSignVo signVo = WxJsapiTiketHelper.getJSapiTiketSign(url);

        result.setData(signVo);
        return result;

    }
}
